<template>
  <a-card :bordered="false">
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :xl="5" :lg="7" :md="8" :sm="24">
            <a-form-item label="项目编号">
              <j-input placeholder="请输入项目编号" v-model="queryParam.xmbh"></j-input>
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="7" :md="8" :sm="24">
            <a-form-item label="项目单号">
              <j-input placeholder="请输入项目单号" v-model="queryParam.itembh"></j-input>
            </a-form-item>
          </a-col>
          <a-col :xl="5" :lg="7" :md="8" :sm="24">
            <a-form-item label="项目名称">
              <j-input placeholder="请输入项目名称" v-model="queryParam.xmmc"></j-input>
            </a-form-item>
          </a-col>
          <a-col :xl="3" :lg="7" :md="5" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>
    <!-- 查询区域-END -->

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="createJsd" type="primary" icon="plus">锁定验收单</a-button>
    </div>

    <!-- table区域-begin -->
    <div>
      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 已选择 <a style="font-weight: 600">{{ selectedRowKeys.length }}</a>项
        <a style="margin-left: 24px" @click="onClearSelected">清空</a>
      </div>
      <a-table
        ref="table"
        size="middle"
        :scroll="{x:true}"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        class="j-table-force-nowrap"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <template slot="htmlSlot" slot-scope="text">
          <div v-html="text"></div>
        </template>
        <template slot="imgSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无图片</span>
          <img v-else :src="getImgView(text)" height="25px" alt="" style="max-width:80px;font-size: 12px;font-style: italic;"/>
        </template>
        <template slot="fileSlot" slot-scope="text">
          <span v-if="!text" style="font-size: 12px;font-style: italic;">无文件</span>
          <a-button
            v-else
            :ghost="true"
            type="primary"
            icon="download"
            size="small"
            @click="downloadFile(text)">
            下载
          </a-button>
        </template>

        <span slot="action" slot-scope="text, record">
          <a @click="handleDetail(record)">详情</a>
        </span>
      </a-table>
    </div>

    <j-modal :visible="xmdsctz" width="400px" @ok="jsdOk" @cancel="jsdCancel" title="锁定验收单"  :confirmLoading="disableSubmit" >
      <a-descriptions size="middle" :column="1" title="确定要锁定这些验收单吗？">
        <a-descriptions-item label="项目编号"> {{xmbh}} </a-descriptions-item>
        <a-descriptions-item label="累计结算金额"> {{jsje}} 元</a-descriptions-item>
        <a-descriptions-item label="累计服务费"> {{fwfje}} 元</a-descriptions-item>
        <a-descriptions-item label="合计(需支付金额)"> <B style="color: #e74c3c"> {{zje}} </B> 元</a-descriptions-item>
      </a-descriptions>
    </j-modal>

    <a-modal
      title="查看验收单"
      :visible="visibleHz"
      width="1000px"
      cancelText="关闭"
      @cancel="HzClose"
      :okButtonProps="{ class:{'jee-hidden': true} }"
    >
      <div  id="hz" style="width:960px;height:600px;">
        <iframe style="width: 100%;height: 100%;" :src="hzImgUrl"></iframe>
      </div>
    </a-modal>

    <a-modal title="验收单锁定失败" width="400px" cancelText="我了解了" :visible="ysdsdsb"
             @cancel="ysdsbClose" :okButtonProps="{ class:{'jee-hidden': true} }">
      只能锁定同一个项目的验收单
    </a-modal>

    <cusdjs-modal ref="modalForm" @ok="modalFormOk"></cusdjs-modal>
  </a-card>
</template>

<script>

  import { httpAction, getAction, postAction } from '@/api/manage'
  import '@/assets/less/TableExpand.less'
  import { mixinDevice } from '@/utils/mixin'
  import { JeecgListMixin } from '@/mixins/JeecgListMixin'
  import CusdjsModal from './modules/CusdjsModal'
  import JDictSelectTag from '@/components/dict/JDictSelectTag.vue'
  import JInput from '@/components/jeecg/JInput'

  export default {
    name: 'CusdjsList',
    mixins:[JeecgListMixin, mixinDevice],
    components: {
      JDictSelectTag,
      CusdjsModal,
      JInput,
    },
    data () {
      return {
        description: '待验收项目单列表',
        disableSubmit: false,
        // 表头
        columns: [
          {
            title: '验收单号',
            dataIndex: 'ysdh',
            align:"center",
          },
          {
            title:'项目编号',
            align:"center",
            dataIndex: 'xmbh'
          },
          {
            title:'项目协议编号',
            align:"center",
            dataIndex: 'xmxybh'
          },
          {
            title:'项目名称',
            align:"center",
            dataIndex: 'xmmc'
          },
          {
            title:'发布时间',
            align:"center",
            dataIndex: 'fbsj',
          },
          {
            title:'项目单号',
            align:"center",
            dataIndex: 'itembh'
          },
          {
            title:'姓名',
            align:"center",
            dataIndex: 'name'
          },
          {
            title:'身份证号',
            align:"center",
            dataIndex: 'identityid'
          },
          {
            title:'手机号',
            align:"center",
            dataIndex: 'phone'
          },
          {
            title:'结算金额',
            align:"center",
            dataIndex: 'fkje',
          },
          {
            title:'结算方式',
            align:"center",
            dataIndex: 'ystype',
            customRender:(text)=> {
              if (text === 1) {
                return '阶段结算'
              }else{
                return '完工结算'
              }
            }
          },
          {
            title:'项目单状态',
            align:"center",
            dataIndex: 'status_dictText'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align:"center",
            fixed:"right",
            width:87,
            scopedSlots: { customRender: 'action' }
          }
        ],
        url: {
          list: "/project/lhygProjectIndividual/getDjslist",
          createJsd: "/project/lhygQyFinance/add",
        },
        dictOptions:{},
        ysdsdsb: false,
        xmdsctz: false,
        visibleHz: false,
        xmbh: '',
        xmmc: '',
        xmbh: '',
        xmxybh: '',
        xmlxid: '',
        fbsj: '',
        fwfl: '',
        ids: '',
        jsje: '',
        fwfje: '',
        zje: '',
        ptzhm: '',
        ptkhh: '',
        ptkh: '',
        qykhh: '',
        qykh: '',
        hzImgUrl: '',
      }
    },
    created() {
    },
    computed: {
    },
    methods: {
      initDictConfig(){
      },
      handleAudit(record){
          this.$refs.modalForm.audit(record);
          this.$refs.modalForm.title = "验收";
          this.$refs.modalForm.disableSubmit = false;
      },
      onSelectChange(selectedRowKeys, selectionRows) {
          this.selectedRowKeys = selectedRowKeys;
          this.selectionRows = selectionRows;
      },
      onClearSelected() {
          this.selectedRowKeys = [];
          this.selectionRows = [];
      },
      createJsd(){
          let that = this
          if(that.selectionRows.length > 0){
              let idstemp = "";
              let errSum = 0
              let firstXmbh = "";
              let jsjeSum = 0;
              let fwfSum = 0;
              that.selectionRows.forEach((item,i) => {
                  if(i==0){
                    firstXmbh = item.xmbh;
                    that.xmlxid = item.xmlxid;
                    that.fwfl = item.fwfl;
                    that.xmmc = item.xmmc;
                    that.xmbh = item.xmbh;
                    that.xmxybh = item.xmxybh;
                    that.fbsj = item.fbsj;
                  }
                  if(item.xmbh != firstXmbh){
                      errSum += 1;
                  } else {
                      idstemp += (item.id + ",");
                      jsjeSum = (parseFloat(jsjeSum) + parseFloat(item.fkje)).toFixed(2);
                      fwfSum = (parseFloat(fwfSum) + parseFloat(item.fkje)*parseFloat(item.fwfl)/100).toFixed(2);
                  }
              })
              if(errSum > 0){
                  that.ysdsdsb = true;
              } else {
                  that.xmbh = firstXmbh;
                  that.ids = idstemp;
                  that.jsje = jsjeSum;
                  that.fwfje = fwfSum;
                  that.zje = (parseFloat(jsjeSum)+parseFloat(fwfSum)).toFixed(2);
                  if(that.zje <= 0){
                      that.ysdsdsb = true;
                  } else {
                      that.xmdsctz = true;
                  }
              }
          } else {
              that.$message.warning("请选择待结算记录后再锁定");
          }
      },
      jsdOk(){
          const that = this
        that.disableSubmit = true;
        that.loading = true;
        postAction(that.url.createJsd, {"ids": that.ids, "jsje": that.jsje, "fwfje": that.fwfje, "zje": that.zje,
          "xmmc": that.xmmc, "xmbh": that.xmbh, "xmxybh": that.xmxybh, "fbsj": that.fbsj, "xmlxid": that.xmlxid}).then((res)=>{
          if(res.success){
            that.xmbh = '';
            that.ids = '';
            that.jsje = '';
            that.fwfje = '';
            that.zje = '';
            that.xmdsctz = false;
            that.onClearSelected();
            that.loadData();
          } else {
            that.$message.error(res.message);
            this.xmdsctz = false;
          }
          that.disableSubmit = false;
          that.loading = false;
        })
      },
      jsdCancel(){
        this.xmbh = '';
        this.ids = '';
        this.jsje = '';
        this.fwfje = '';
        this.zje = '';
        this.onClearSelected();
        this.xmdsctz = false;
      },
      HzClose(){
          this.visibleHz = false;
      },
      pdfShow(record){
          this.hzImgUrl = record.ysdpdf;
          this.visibleHz = true;
      },
      ysdsbClose(){
          this.ysdsdsb = false;
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less';
</style>